﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace win1
{
    public partial class Update : Form
    {
        public int id;
        DataTable tb = null;
        private static string connStr = "Data Source=.\\MSSQLSERVER3;Initial Catalog=SecondHandCarDB;Persist Security Info=True;database=Demo;User ID=sa;Password=root";
        public Update()
        {
            InitializeComponent();
        }

        //窗体加载事件
        private void Update_Load(object sender, EventArgs e)
        {
            binding();
        }

        public void binding()
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlDataReader sdr = null;
            try
            {
                conn.Open();
                string sql = "select * from [user] where id = " + id;
                SqlCommand cmd = new SqlCommand(sql, conn);
                sdr = cmd.ExecuteReader();
                if (sdr.HasRows)
                {
                    if (sdr.Read())
                    {
                        string username = (string)sdr[1];
                        string password = (string)sdr[2];

                        this.textBox1.Text = username;
                        this.textBox2.Text = password;
                    }
                }
                else
                {
                    MessageBox.Show("暂无数据", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生了异常：" + ex.Message, "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                sdr.Close();
                conn.Close();
            }


        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        //点名修改
        private void button1_Click(object sender, EventArgs e)
        {
            String username = this.textBox1.Text.Trim();
            String password = this.textBox2.Text.Trim();
            String sql = string.Format("update [user] set username = '{0}',password = '{1}' where id = '{2}'",username,password,id);
            int rowCount = DataBase.ExecuteU(sql);
            if (rowCount > 0)
            {
                MessageBox.Show("修改成功！", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败！", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }

        }
    }
}
